package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.RoomInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Autowired
    private RoomInfoService roomInfoService;

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        roomInfoService.saveOrUpdateRoomInfo(roomSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current,
                                              @RequestParam long size,
                                              RoomQueryVo queryVo) {
        Page<Object> page = new Page<>(current, size);
        IPage<RoomItemVo> result = roomInfoService.pageItem(page, queryVo);
        return Result.ok(result);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomDetailVo = roomInfoService.getDetailById(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        // roomInfoService调用自身的removeById方法只对自身定义的RoomInfo有效，所以需要自定义方法实现
        roomInfoService.removeByRoomId(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        UpdateWrapper<RoomInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("is_release", status);
        roomInfoService.update(updateWrapper);
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {

        //  List<RoomInfo> roomInfos = roomInfoService.list(new QueryWrapper<RoomInfo>().eq("apartment_id", id));
        LambdaUpdateWrapper<RoomInfo> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        queryWrapper.eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED); // 发布状态才能显示
        List<RoomInfo> allRoomInfos = roomInfoService.list(queryWrapper);
        // notice : 此时还需要判断房间状态，status in (2,5,7) 时为不可预约，status in (1,3,4，6) 时为可预约
        List<Long> statusIds = roomInfoService.getStatusIds(id); // 在（2，5，7）中的 room_id
        // 在allRoomInfos中删除掉statusIds中的room_id
        // 移除不可预约的room_id Java8 特性
       // allRoomInfos.removeIf(roomInfo -> statusIds.contains(roomInfo.getId()));
        // 移除不可预约的room_id Stream()
        List<RoomInfo> collect = allRoomInfos.stream()
                // 判断room_id是否在statusIds中,allRoomInfos.contains(allRoomInfo.getId()保存匹配信息，所以取反
                                    .filter(allRoomInfo -> !allRoomInfos.contains(allRoomInfo.getId()))
                                    .collect(Collectors.toList());

        return Result.ok(collect);
    }

}


















